﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Reflection;
using devtm.Helpers;
using System.Windows.Forms;
using System.Diagnostics;
using devtm.Collections;

namespace devtm.Types.LazyLoading
{
    public class AssemblyFileNamespace : AssemblyNamespace
    {



        /// <summary>
        /// Initializes a new instance of the <see cref="AssemblyFileNamespace"/> class.
        /// </summary>
        /// <param name="file">The file.</param>
        public AssemblyFileNamespace(FileInfo file)
            : base()
        {
            this.Filename = file.FullName;

            SingleName = file.Name;

            SingleName = SingleName.Substring(0, SingleName.Length - file.Extension.Length);
            Name = SingleName;

            this.file = file;
            IsFile = true;
            IsInError = false;

            Debug.Print(file.DirectoryName);

            if (file.DirectoryName.Contains(@"\Temporary "))
                Hide = true;

            LoadReferences();

        }


        
        private void LoadReferences()
        {

            if (ReferencesLoaded)
                return;

            Assembly ass = Assembly.LoadFrom(Filename);
            System.Reflection.AssemblyName[] names = ass.GetReferencedAssemblies();

            foreach (System.Reflection.AssemblyName name in names)
            {
                AssemblyDescription desc = new AssemblyDescription(name.FullName);
                desc.HashCode = name.GetHashCode();
                this.References.Add(desc);
            }

            ReferencesLoaded =true;

        }


        private FileInfo file;

        public override void Initialize()
        {
            if (this.Initialized || IsInError)
                return;

            try
            {

                this.Dirs = LazyLoadingHelper.Directories;
                Assembly ass = System.Reflection.Assembly.LoadFrom(file.FullName);
                this.Initialized = true;
                this.Version = ass.ImageRuntimeVersion;
                Name = new AssemblyDescription(ass.FullName).ToSmallString();
                PropertyChange("Name");
            }
            catch (Exception ex)
            {
                
                IsInError = true;
                if (ex.Message.StartsWith("Mixed mode assembly is built against version"))
                {
                    MessageBox.Show(
                        ex.Message,
                        "Error",
                        MessageBoxButtons.OK,
                        MessageBoxIcon.Warning);

                    return;
                }
            }
        }


        private bool IsValid()
        {

            Assembly ass = Assembly.LoadFrom(Filename);
            System.Reflection.AssemblyName[] names = ass.GetReferencedAssemblies();

            foreach (System.Reflection.AssemblyName name in names)
            {
                AssemblyDescription desc = new AssemblyDescription(name.FullName);
                this.References.Add(desc);
            }

            return true;

        }

   
        public bool Hide { get; set; }
    }

}
